using Godot;
using System;
using Common;
namespace Library {
	public class Fader : ColorRect {
		public new Color Color {
			get => new Color(base.Color.r, base.Color.g, base.Color.b, 1);
			set => base.Color = new Color(value.r, value.g, value.b, base.Color.a);
		}
		public float Alpha {
			get => base.Color.a;
			set => base.Color = new Color(base.Color.r, base.Color.g, base.Color.b, value);
		}

		bool fading=false;
		float from=0;
		float to=0;
		float duration=0;
		float time=0;
		public void FadeTo(float to, float duration)=>Fade(Alpha,to,duration);
		public void Fade(float from,float to,float duration){
			fading=true;
			this.from=from;
			this.to=to;
			this.duration=duration;
			time=0;
		}
		
		public override void _Process(float delta) {
			if(fading){
				time+=delta;
				if(time>=duration){
					Alpha=to;
					fading=false;
				}else{
					Alpha=Easing.Linear(time,duration,from,to);
				}
			}
		}
	}
}
